<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class ChangeRoleForm extends CFormModel
{
    public $data;
    
    public function changeRole($isAdmin){
        $user = new User();
        $ids = "";
        $count = 0;
        foreach($this->data as $index=>$item){
            $id = $item->id;
            if(strlen($ids)>0){
                $ids = $ids.",";
            }
            $ids = $ids.$id;
            $count = $count+1;
        }
        if(strlen($ids)>0){
            if($isAdmin==false && $this->isLastAdmin()){
                                return 0;
            }
            $user = new User();
            $user->changeRole($ids,$isAdmin);
        }
        return $count;
    }
    
    public function isLastAdmin(){
        $ids = "";
        foreach($this->data as $index=>$item){
            $id = $item->id;
            if(strlen($ids)>0){
                $ids = $ids.",";
            }
            $ids = $ids.$id;
        }
        if(strlen($ids)>0){
            $userMeta = new UserMeta();
            return $userMeta->isLastAdmin($ids);
        }
        return false;
    }
    public function search()
    {
        return new UserListDataProvider(new UserListForm(), array(
        ));
    }
}
